﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Odyssey.Controls;
using Odyssey.Controls.Classes;
using System.Windows.Controls.Primitives;
using WpfELearningAuthoringTool;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;
using System.Collections.ObjectModel;
using ScormModel.Question;
namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for TrueFalseQuestionDialog.xaml
    /// </summary>
    public partial class TrueFalseQuestionDialog : Window
    {
        RibbonBarRegion.RibbonBarHeader parentChild;
        ObservableCollection<QuestionAnswer> listAnswer = new ObservableCollection<QuestionAnswer>(); 
        string currentDirectory;
        WebBrowser browser;
        ImsManifest imsmanifest;
        bool isAddNew = true;
        QuestionTest questionTest;
        public QuestionTest QuestionTest
        {
            get { return this.questionTest; }
            set { this.questionTest = value; }
        }
        public bool IsAddNew
        {
            get { return this.isAddNew; }
            set { this.isAddNew = value; }
        }
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public WebBrowser WebBrowser
        {
            get { return this.browser; }
            set { this.browser = value; }
        }
        public RibbonBarRegion.RibbonBarHeader ParentCHild
        {
            get { return this.parentChild; }
            set { this.parentChild = value; }
        }
        public TrueFalseQuestionDialog()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (rbTrue.IsChecked == false && rbFalse.IsChecked == false)
            {
                MessageBox.Show("Please select answer");
                return;
            }

            if (rbTrue.IsChecked == true)
            {
                QuestionAnswer qaTrue = new QuestionAnswer();
                qaTrue.Id = "1";
                qaTrue.Answer = "true";
                qaTrue.Correct = true;
                listAnswer.Add(qaTrue);
                QuestionAnswer qaFalse = new QuestionAnswer();
                qaFalse.Id = "2";
                qaFalse.Answer = "false";
                qaFalse.Correct = false;
                listAnswer.Add(qaFalse);
            }
            else {
                QuestionAnswer qaTrue = new QuestionAnswer();
                qaTrue.Id = "1";
                qaTrue.Answer = "true";
                qaTrue.Correct = false;
                listAnswer.Add(qaTrue);
                QuestionAnswer qaFalse = new QuestionAnswer();
                qaFalse.Id = "2";
                qaFalse.Answer = "false";
                qaFalse.Correct = true;
                listAnswer.Add(qaFalse);
            }
            if (isAddNew == false)
            {
                ChoiceQuestionData singleChoiceQuestion = new ChoiceQuestionData();
                singleChoiceQuestion.QuestionContent = new TextRange(txtQuestionContent.Document.ContentStart, txtQuestionContent.Document.ContentEnd).Text;
                singleChoiceQuestion.QuestionHint = new TextRange(txtQuestionHint.Document.ContentStart, txtQuestionHint.Document.ContentEnd).Text;
                singleChoiceQuestion.ListPossibleAnswer = new List<QuestionAnswer>();
                for (int i = 0; i < listAnswer.Count; i++)
                {
                    QuestionAnswer qi = listAnswer[i];
                    singleChoiceQuestion.ListPossibleAnswer.Add(qi);
                }
                singleChoiceQuestion.Type = "true_false";
                singleChoiceQuestion.Identifier = "item";
                singleChoiceQuestion.CurrentDirectory = currentDirectory;
                questionTest.QuestionData = singleChoiceQuestion;
                Close();
            }
            else
            {
                try
                {
                    ChoiceQuestionData singleChoiceQuestion = new ChoiceQuestionData();
                    singleChoiceQuestion.QuestionContent = new TextRange(txtQuestionContent.Document.ContentStart, txtQuestionContent.Document.ContentEnd).Text;
                    singleChoiceQuestion.QuestionHint = new TextRange(txtQuestionHint.Document.ContentStart, txtQuestionHint.Document.ContentEnd).Text;
                    singleChoiceQuestion.ListPossibleAnswer = new List<QuestionAnswer>();
                    for (int i = 0; i < listAnswer.Count; i++)
                    {
                        QuestionAnswer qi = listAnswer[i];
                        singleChoiceQuestion.ListPossibleAnswer.Add(qi);
                    }
                    singleChoiceQuestion.Type = "true_false";
                    singleChoiceQuestion.Identifier = "item";
                    singleChoiceQuestion.CurrentDirectory = currentDirectory;
                    HtmlDocument content = singleChoiceQuestion.CreateHtmlNode();
                    //using (StreamWriter writer = File.CreateText(currentDirectory + "/" + "Test1.htm"))
                    //{
                    //    writer.Write(content.HTML);
                    //    writer.Close();
                    //    browser.NavigateToString(content.HTML);
                    //}
                    browser.NavigateToString(content.HTML);
                    Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
